<template>
  <div class="main-card">
    <el-card class="box-card">
      <template #header>
        <span>字典管理</span>
      </template>
      <el-form ref="ruleForm" :model="queryParams" label-width="100px">
        <div class="search-form">
          <el-form-item label="字典名称">
            <el-input v-model="queryParams.dictName" clearable />
          </el-form-item>
          <el-form-item label="字典类型">
            <el-input v-model="queryParams.dictType" clearable />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="queryParams.enable" clearable placeholder="请选择">
              <el-option
                label="正常"
                :value="1"
              />
              <el-option
                label="停用"
                :value="0"
              />
            </el-select>
          </el-form-item>
          <el-button style="margin-left: 20px" type="primary" @click="search">搜索</el-button>
        </div>
      </el-form>
      <div style="margin:20px 0;">
        <el-button v-hasPermi="['system:dict:add']" type="primary" @click="addOrUpdate()">添加字典</el-button>
        <el-button v-hasPermi="['system:dict:remove']" type="danger" @click="deleteDictType">删除字典</el-button>
        <el-button v-hasPermi="['system:dict:remove']" type="warning" @click="clearCache">清理缓存</el-button>
      </div>
      <div>
        <el-table
          :data="dataList"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column
          prop="dictId"
          label="字典编号"
          width="120"
        />
        <el-table-column
          prop="dictName"
          label="字典名称"
          width="120"
        />
        <el-table-column label="字典类型" width="150" :show-overflow-tooltip="true">
          <template #default="{row}">
            <router-link :to="'/system/DictType/data/' + row.dictId" class="link-type">
              <span>{{ row.dictType }}</span>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="enable"
          label="状态"
          width="120"
        >
          <template #default="{row}">
            {{ row.enable ? '正常' : '停用'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="120"
          :formatter="dateFormat"
        />
        <el-table-column
          prop="remark"
          label="备注"
          width="120"
        />
        <el-table-column label="操作" width="250">
          <template v-slot="{ row }">
            <el-button v-hasPermi="['system:dict:edit']" type="primary" @click="addOrUpdate(row)">修改</el-button>
            <el-button v-hasPermi="['system:dict:remove']" type="danger" @click="deleteDictType(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        v-show="total>0"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
      />
      </div>
    </el-card>
    <au-dict-type :value="isAuDictTypeShow" @close="isAuDictTypeShow = false" @onRefresh="search"/>
  </div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent } from 'vue'
import { useStore } from 'vuex'
import { ElMessageBox, ElMessage } from 'element-plus'
import AuDictType from './components/AuDictType.vue'
import tableList, { BaseQueryType, BaseDataType } from '@/mixins/tableList'
import { DictType, StateType as DictStateType } from '@/store/modules/system/dictType'

interface QueryParamsType extends BaseQueryType{
  dictName: string;
  dictType: string;
  enable: number;
}

interface DataType extends BaseDataType<DictType> {
  isAuDictTypeShow: boolean;
  queryParams: QueryParamsType;
}

export default defineComponent({
  name: 'DictType',
  components: { AuDictType },
  setup() {
    const store = useStore<{dictType: DictStateType}>()
    const data = reactive<DataType>({
      url: 'dictType/getDictTypeList',
      isAuDictTypeShow: false,
      dataList: [],
      queryParams: {
        dictName: '',
        dictType: '',
        enable: 1,
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      selectList: []
    })
    const {
      dateFormat,
      getList,
      handleSelectionChange,
      handleCurrentChange,
      search
    } = tableList(data)
    const addOrUpdate = (row: DictType) => {
      store.commit('dictType/SET_DATA', row)
      data.isAuDictTypeShow = true
    }
    const deleteDictType = (row: DictType) => {
      const dictIds = row.dictId || (data.selectList || []).map(item => item.dictId)
      ElMessageBox.confirm('您确定要删除"' + dictIds + '"字典吗, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return store.dispatch('dictType/deleteDictType', dictIds)
      }).then(() => {
        ElMessage({
          message: '删除成功',
          type: 'success'
        })
        getList.value()
      }).catch()
    }
    const clearCache = () => {
      ElMessageBox.confirm('您确定要清空缓存吗, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return store.dispatch('dictType/clearCache')
      }).then(() => {
        ElMessage({
          message: '清空缓存成功',
          type: 'success'
        })
        getList.value()
      }).catch()
    }

    search.value()
    return {
      dateFormat,
      getList,
      handleSelectionChange,
      handleCurrentChange,
      search,
      ...toRefs(data),
      addOrUpdate,
      deleteDictType,
      clearCache
    }
  }
})
</script>

<style lang="scss">
.el-pagination{
  margin-top: 20px;
}
</style>
